//Copyright 2012 Trevin Liberty
//GNU GPL v3
//Author: Trevin Liberty

#include "GUI.h"
#include <sstream>
#include <string>

using std::stringstream;
using std::string;

GUI::GUI(int screenWidth, int screenHeight) {
	this->screenWidth = screenWidth;
	this->screenHeight = screenHeight;
	miniMap = al_load_bitmap("rsc/GUI/Map.tga");
	sideBar = al_load_bitmap("rsc/GUI/SideBar.tga");
	scoreBoard = al_load_bitmap("rsc/GUI/scoreBoard.tga");
	font = al_load_font("rsc/Font/8bitlim.ttf", 22, 0);

	barracksSelected = false;

	mapBounds = Point<int>(225, 225);
	sideBarBounds = Point<int>(200, 500);

	sideBarPos = Point<int>(screenWidth - 212, 100);
}

Build GUI::getNewButtonPressBuilding(Point<int> cursorXY) {
	if(barracksSelected) {
		if(cursorXY.x > screenWidth - 185 && cursorXY.x < screenWidth - 121 &&
			cursorXY.y > 240 && cursorXY.y < 304) {
				return bow;
		}
		if(cursorXY.x > screenWidth - 110 && cursorXY.x < screenWidth - 46 &&
			cursorXY.y > 240 && cursorXY.y < 304) {
				return axe;
		}
	}

	return null;
}

void GUI::draw(list<Entity*>* entities, Point<float> camPos, PlayerResources* playerRes) {

	if(barracksSelected) {
		if(sideBarPos.y + 5 <= 169) {
			sideBarPos.y += 5;
		}
		al_draw_bitmap(sideBar, screenWidth - 212, sideBarPos.y, 0);
	}
	else {
		if(sideBarPos.y - 5 >= 95) {
			sideBarPos.y -= 5;
		}
		al_draw_bitmap(sideBar, screenWidth - 212, sideBarPos.y, 0);
	}

	list<Entity*>::iterator it = entities->begin();

	al_draw_bitmap(miniMap, screenWidth - 225, 0, 0);

	Point<float> pos;
	for(it; it != entities->end(); ++it) {
		if((*it) != NULL && !(*it)->shouldRemove()) {
			pos = (*it)->getPosition();
			pos.x = pos.x*.06875 + screenWidth - 225;
			pos.y = pos.y*.06875;
			if((*it)->getTeam() == 0)
				al_draw_rectangle(pos.x, pos.y, pos.x + 1, pos.y + 1, al_map_rgb(25, 255, 25), 1);
			else
				al_draw_rectangle(pos.x, pos.y, pos.x + 1, pos.y + 1, al_map_rgb(225, 25, 25), 1);
		}
	}
	Point<float> size;
	size.x = screenWidth*.0625;
	size.y = screenHeight*.0625;
	pos.x = camPos.x*.0625 + screenWidth - 220;
	pos.y = camPos.y*.0625 + 7;
	al_draw_rectangle(pos.x, pos.y, pos.x + size.x + 10, pos.y + size.y + 10, al_map_rgb(100, 100, 100), 1);

	float halfScreenX = screenWidth/2;

	al_draw_bitmap(scoreBoard, halfScreenX - 225, 0 , 0);

	string s;
	stringstream oss;
	oss << playerRes->kills;
	s = oss.str();
	oss.str("");

	al_draw_text(font, al_map_rgb(255, 0, 0), halfScreenX - 120, 15, 0, s.c_str());

	oss << playerRes->deaths;
	s = oss.str();
	oss.str("");

	int length = al_get_text_width(font, s.c_str());
	int posX = halfScreenX + 115 - length;

	al_draw_text(font, al_map_rgb(255, 0, 0), posX, 15, 0, s.c_str());

	oss << playerRes->gold;
	s = oss.str();
	oss.str("");

	al_draw_text(font, al_map_rgb(253, 254, 0), halfScreenX - 25, 23, 0, s.c_str());
}